package com.aiwei.tdjk.controller;

import com.aiwei.tdjk.common.Constants;
import com.aiwei.tdjk.dto.request.GetRechargeBillPagerRequest;
import com.aiwei.tdjk.entity.RechargeBill;
import com.aiwei.tdjk.enums.CheckStateEnum;
import com.aiwei.tdjk.enums.RechargeTypeEnum;
import com.aiwei.tdjk.service.RechargeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin/rechargeBill")
public class RechargeBillController extends BaseController {

	@Autowired
	private RechargeBillService rechargeBillService;


	protected void setCommonData(Model model) {
		model.addAttribute("checkStateList", CheckStateEnum.values());
		model.addAttribute("rechargeTypeList", RechargeTypeEnum.values());
	}


	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	public String list(GetRechargeBillPagerRequest request, Model model) {
		model.addAttribute("page", rechargeBillService.find(request));
		model.addAttribute("user", request);
		setCommonData(model);
		return viewName("list");
	}


	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String showCreateForm(Model model) {
		setCommonData(model);
		model.addAttribute(Constants.OP_NAME, "新增");
		if (!model.containsAttribute("m")) {
			RechargeBill rechargeBill = new RechargeBill();
			model.addAttribute("m", rechargeBill);
		}
		return viewName("editForm");
	}


	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(Model model, @Valid @ModelAttribute("m") RechargeBill m, BindingResult result,
			RedirectAttributes redirectAttributes) {

		if (hasError(result)) {
			return showCreateForm(model);
		}
		rechargeBillService.save(m);
		redirectAttributes.addFlashAttribute(Constants.MESSAGE, "新增成功");
		return redirectToUrl("list");
	}

	@RequestMapping(value = "{sn}/update", method = RequestMethod.GET)
	public String showUpdateForm(@PathVariable("sn") String sn, Model model) {
		setCommonData(model);
		model.addAttribute(Constants.OP_NAME, "修改");
		model.addAttribute("m", rechargeBillService.getBySN(sn));
		return viewName("editForm");
	}

	@RequestMapping(value = "{sn}/update", method = RequestMethod.POST)
	public String update(Model model, @Valid @ModelAttribute("m") RechargeBill m, BindingResult result,
			RedirectAttributes redirectAttributes) {

		if (hasError(result)) {
			return showUpdateForm(m.getSn(), model);
		}
		//修改基本信息
		rechargeBillService.update(m);
		redirectAttributes.addFlashAttribute(Constants.MESSAGE, "修改成功");
		return redirectToUrl("list");
	}


	@RequestMapping(value = "{sn}/delete", method = RequestMethod.GET)
	public String showDeleteForm(@PathVariable("sn") String sn, Model model) {
		setCommonData(model);
		model.addAttribute(Constants.OP_NAME, "删除");
		model.addAttribute("m", rechargeBillService.getBySN(sn));
		return viewName("editForm");
	}

	@RequestMapping(value = "{sn}/delete", method = RequestMethod.POST)
	public String delete(@PathVariable("sn") String sn,RedirectAttributes redirectAttributes) {
		rechargeBillService.deleteBySn(sn);
		redirectAttributes.addFlashAttribute(Constants.MESSAGE, "删除成功");
		return redirectToUrl("list");
	}




}
